import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:easy_refresh/easy_refresh.dart';
import '../../bean/pay_list_entity.dart';
import '../../utils/common.dart';
import '../../utils/constant.dart';
import '../../utils/network_manager.dart';
import '../../utils/network_url.dart';
import '../../utils/toast_util.dart';
import '../../utils/user_util.dart';
import '../../widgets/myappbar.dart';
import 'package:get/get.dart';

class LogsController extends GetxController{
  String title = '';
  @override
  void onInit() {
    super.onInit();
    title = Get.arguments;
    _getService();
  }
  @override
  void onClose() {
    super.onClose();
  }
  List<PayListData> list = [];
  void _getService(){
    DioManager.getInstance().post(
        title=='充值记录'?
        ServiceUrl.inList
        :       ServiceUrl.outList
        ,{
      "user_name": UserUtil.getUSER()?.userName
    }, (data) async {
      PayListEntity bannerEntity = PayListEntity.fromJson(jsonDecode(data));
      list = bannerEntity.data;
      update();
    }, (error) {
      ToastUtil.show(error);
    });
  }
  EasyRefreshController easyRefreshController = EasyRefreshController(
    controlFinishRefresh: false,
    controlFinishLoad: true,
  );
}

class LogsPage extends GetView<LogsController>  {


  late LogsController controller ;
  late BuildContext context ;
  @override
  Widget build(BuildContext context1) {
    Get.lazyPut<LogsController>(() => LogsController());
    controller = Get.find();
    context = context1;

    return GetBuilder<LogsController>(
      builder: (_){
        return  Scaffold(
            appBar: MyAppBar(context: context, title: controller.title,hasLeading: true,),
            backgroundColor: Colors.white,
            body: controller.list.length==0?_empty(): EasyRefresh(
              header: ClassicHeader(
                dragText: 'Pull to refresh'.tr,
                armedText: 'Release ready'.tr,
                readyText: 'Refreshing...'.tr,
                processingText: 'Refreshing...'.tr,
                processedText: 'Succeeded'.tr,
                noMoreText: 'No more'.tr,
                failedText: 'Failed'.tr,
                messageText: 'Last updated at %T'.tr,
              ),
              footer: ClassicFooter(
                dragText: 'Pull to load'.tr,
                armedText: 'Release ready'.tr,
                readyText: 'Loading...'.tr,
                processingText: 'Loading...'.tr,
                processedText: 'Succeeded'.tr,
                noMoreText: 'No more'.tr,
                failedText: 'Failed'.tr,
                messageText: 'Last updated at %T'.tr,
              ),
              controller: controller.easyRefreshController,
              onRefresh: () async {
              },
              onLoad: () async {
                controller.easyRefreshController.finishLoad(
                    IndicatorResult.noMore );
              },
              child: ListView.builder(
                itemBuilder: (context, index) {
                  return _item(controller.list[index]);
                },
                itemCount: controller.list.length,
              ),
            ));
      },
    );

  }


  Widget _empty(){
    return Container(
      alignment: Alignment.center,
      child: Image.asset(Constant.IMG_BASE+'empty.png',width: 150.w,height: 127.h,),
    );
  }

  Widget _item(PayListData item){
    return Container(
      margin: EdgeInsets.only(top: 6.h),
      width: MediaQuery.of(context).size.width - 32.w,
      padding: EdgeInsets.only(left: 16.w,right: 16.w),
      height: 76.h,
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.r),
          border: Border.all(color: Color(0x0D000000),width: 1.w)
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(item.way,style: TextStyle(
                  color: Color(0xFF333333),
                  fontSize: 14.sp
              ),),
              Text( controller.title=='充值记录'? Common.patStatusToText(item.status):Common.patStatusToText2(item.status),style: TextStyle(
                  color: Color(0xFF999999),
                  fontSize: 13.sp
              ),),
            ],
          ),
          SizedBox(height: 7.h,),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(item.time,style: TextStyle(
                  color: Color(0xFF999999),
                  fontSize: 13.sp
              ),),
              Text(item.amount.toString(),style: TextStyle(
                  color: Color(0xFF007BE5),
                  fontWeight: FontWeight.bold,
                  fontSize: 15.sp
              ),),
            ],
          ),

        ],
      ),
    );
  }

}